/*******************************************************************************
*  Project      : Minigate Golfin                                 
*  Program ID   : club_view.js                                 
*  Description  : 개별클럽 일반게시판 상세보기
*
********************************************************************************
*  Program History                                                            
*  Date        Author    Description                                          
*  ----------  --------  -------------------------------------------------------- 
*  2012-05-11  이윤노    Created.  
*  2012-05-14  이윤노    Complete.  
*******************************************************************************/

jQuery(function($){
	// 상단 타이틀
	function clsClubTitle()
	{
		this._HTMLDom = null;
		
		this._init();
	}
	
	clsClubTitle.prototype = {
		_init: function(){
			var $layer = $("div#container>section#main_content>div.golf_board article.depth2_tab");
			
			this._HTMLDom = {
				"$layer": $layer
				, "$tabs": $layer.find("li")
			}
			
			$layer.data("idx", null); // 인덱스로 관리
			
			this.display();
		}
		, setIdx: function(val){
			
			var $layer = this._HTMLDom["$layer"];
			
			if(val == $layer.data("idx")) return false;
			
			$layer.data("idx", val);
			
			this.display();
			return true;
		}
		, display: function(){
			var $layer = this._HTMLDom["$layer"], $tabs = this._HTMLDom["$tabs"];
			var idx = $layer.data("idx");
			
			// 현재 인덱싱된 타이틀만 on설정
			$tabs.find("img").each(function(n){
				var imgSrc = $(this).attr("src");
				var szImgName = imgSrc.replace(/(.*)\/(.*).gif/g, function(){
					return arguments[1] + "/" + arguments[2].replace("_on", "");
				});
				
				$(this).attr("src", szImgName + ((idx == n) ? "_on":"") + ".gif");
			});
		}
		// 탭 버튼
		, $getsBtnTab: function(){
			var $layer = this._HTMLDom["$layer"], $tabs = this._HTMLDom["$tabs"];
			
			return $tabs.find("a");
		}
	}
	
	//상단메뉴
	function clsTopMenuInfo(){
		this._HTMLDom = null;
		
		this._init();
	}
	
	clsTopMenuInfo.prototype = {
		_init: function(){
			var $layer = $("div#container nav ul.self_club_gnb");			
			this._HTMLDom = {
				"$layer":$layer
			}			
		}
		, getBtnClubHome: function(){
			var $layer = this._HTMLDom["$layer"];
			return $layer.find("li:first");
		}
		, getBtnClubField: function(){
			var $layer = this._HTMLDom["$layer"];
			return $layer.find("li:eq(1)");
		}
		, getBtnClubBoard: function(){
			var $layer = this._HTMLDom["$layer"];
			return $layer.find("li:eq(2)");
		}
		, getBtnClubPic: function(){
			var $layer = this._HTMLDom["$layer"];
			return $layer.find("li:eq(3)");
		}
		, getBtnClubAdmin: function(){
			var $layer = this._HTMLDom["$layer"];
			return $layer.find("li:eq(4)");
		}
	}
	
	// 클럽게시판상세
	function clsClubInfo()
	{
		this._HTMLDom = null;
		this.infoData = null;
		
		this._init();
	}
	
	clsClubInfo.prototype = {
		_init: function(){
			var $layer = $("div#container section#main_content div.golf_board article.board_view")
			this._HTMLDom = {
				"$layer": $layer
			}
		}
		, setInfo: function(thisData){
			this.infoData = thisData;
			this.display();
		}
		, display: function(){
			var $layer = this._HTMLDom["$layer"];
			
			
			
			$layer.find("dl.view_tit dt").contents().eq(0).remove().end().end().prepend(this.infoData["제목"]);
			if(this.infoData["댓글수"]) $layer.find("dl.view_tit dt span").html("(" + this.infoData["댓글수"] + ")");
			$layer.find("dl.view_tit dd span:eq(0) img").attr("src", this.infoData["작성자"]["이미지"]);
			$layer.find("dl.view_tit dd span:eq(1)").html(this.infoData["작성자"]["닉네임"]);
			$layer.find("table thead th:eq(1) span:eq(1)").contents().eq(0).remove().end().end().prepend(this.infoData["작성일"])
			$layer.find("table thead th:eq(1) span:eq(1) span").html(this.infoData["작성시"]);
			$layer.find("table thead th:eq(1) span:eq(1)").html(this.infoData["작성일"] + "<span>" + this.infoData["작성시"] + "</span>");
			$layer.find("table thead th:eq(2) span:eq(1)").html(this.infoData["조회수"]);
			$layer.find("table tbody td").html(this.infoData["내용"]);
			//alert(this.infoData["내용"])
		
		}
		, spinStart: function(){
			var $layer = this._HTMLDom["$layer"];
			
			$layer.find("dl:first, table:first").hide();
			if($layer.find("div#tmpSpin").length == 0){
				$layer.prepend("<div id='tmpSpin' style='position:relative; width:600px; height:100px;'></div>");
			}
			$layer.find("div#tmpSpin").show().spin();
		}
		, spinEnd: function(){
			var $layer = this._HTMLDom["$layer"];
			
			$layer.find("div#tmpSpin").hide().spin(false);
			$layer.find("dl, table").show();
		}
	}
	
	// 클럽게시판댓글
	function clsClubCommentList()
	{
		this._HTMLDom = null;
		this.listData = null;
		this._ERRMSG = {
			"_NOVALUE": "제목을 입력해 주십시요."
		}
		this.errMsg = null;
		
		this._init();
	}
	
	clsClubCommentList.prototype = {
		_init: function(){
			var $layer = $("div#container section#main_content div.golf_board article.board_view div.reply_area")
			this._HTMLDom = {
				"$layer": $layer
				, "$element": $layer.find("dl:first").clone(true)
			}
			
			$layer.find("dl").remove();
		}
		, setList: function(thisData){
			this.listData = thisData;
		
			this.display();
		}
		, display: function(){
			var $layer = this._HTMLDom["$layer"];
			var $element = this._HTMLDom["$element"];
			var thisClass = this;
			
			$layer.find("dl").remove();
			for(var i=0;i<this.listData.length;i++){
				$layer.find("div.reply_regist").before($element.clone(true));
			}
			
			$layer.find("dl").each(function(n){				
				$(this).find("dt img").attr("src", thisClass.listData[n]["작성자"]["이미지"]);
				$(this).find("dd p.nickname a").html(thisClass.listData[n]["작성자"]["닉네임"]).click(function(event){
					alert('a');
				});
				$(this).find("dd p.reply_date").html(thisClass.listData[n]["작성일"] + "<span>" + thisClass.listData[n]["작성시"] + "</span>");
				$(this).find("dd div").html(thisClass.listData[n]["내용"]);
				//alert(thisClass.listData[n]["작성일"]);
				/* 
				if(thisClass.listData[n]["삭제유무"]=="Y") {
					$(this).find("dd p.reply_del a").click(function(event){
						
					});
				}else{
					$(this).find("dd p.reply_del").hide();
				};*/
			});
			
			
		}
		
		, spinStart: function(){
			var $layer = this._HTMLDom["$layer"];
			
			if($layer.find("div#tmpSpin").length == 0){
				$layer.find("p.white_line").after("<div id='tmpSpin' style='position:relative; width:600px; height:100px;'></div>");
			}
			$layer.find("div#tmpSpin").show().spin();
			
			$layer.find("dl").hide();
		}
		, spinEnd: function(){
			var $layer = this._HTMLDom["$layer"];
			
			$layer.find("div#tmpSpin").hide().spin(false);
			
			$layer.find("dl").show();
		}
		, $getBtnCommentDelete: function(){
			$layer = this._HTMLDom["$layer"];
			return $layer.find("p.reply_del");
		}
		, getCommentData: function(val){
			return this.listData[val];
		}
		, getCommentSubmitData: function(){
			$layer = this._HTMLDom["$layer"];			
			return $layer.find("div.reply_regist textarea").val();
		}
		, setCommentSubmitData: function(val){
			$layer = this._HTMLDom["$layer"];			
			$layer.find("div.reply_regist textarea").val(val);
		}
		, $getBtnCommnetSubmit: function(){
			$layer = this._HTMLDom["$layer"];			
			return $layer.find("div.reply_regist a");
		}
		, isValid: function(){
			$layer = this._HTMLDom["$layer"];			
			return $layer.find("div.reply_regist textarea").val();
		}
	}
	
	// 수정삭제
	function clsAuthButton()
	{
		this._HTMLDom = null;
		this.bAuth = null;
		
		this._init();
	}
	
	clsAuthButton.prototype = {
		_init: function(){
			var $layer = $("div#container section#main_content div.golf_board article.btn_area_box");
			this._HTMLDom = {
				"$layer": $layer
			}
			
			$layer.find("a").has("img[alt=수정], img[alt=삭제]").hide();
		}
		, setAuth: function(bAuth){
			this.bAuth = bAuth;
			this.display();
		}
		, display: function(){
			if(this.bAuth) this._HTMLDom["$layer"].find("a").has("img[alt=수정], img[alt=삭제]").show();
		}
		, $getBtnModify: function(){
			return this._HTMLDom["$layer"].find("a").has("img[alt=수정]");
		}
		, $getBtnDelete: function(){
			return this._HTMLDom["$layer"].find("a").has("img[alt=삭제]");
		}
	}
	
	function clsComm()
	{
		this.clubRequest = null;
		this.clubData = null;
		
		this.clubCommentList = null;
		this.clubCommentRequest = null;
		
		this._init();
	}
	
	clsComm.prototype = {
		_init: function(){
			this.clubData = {
				"제목": null
				, "댓글수": null
				, "작성자": {
						"아이디": null
						, "이미지": null
						, "닉네임": null
					}
				, "작성일": null
				, "작성시": null
				, "조회수": null
				, "내용": null
			}
			
			this.clubRequest = {
				"클럽키": null
				, "게시판키": null
				, "전체글/내글": null
			}
			
			this.clubCommentList = [
				{
					"키": null
					, "작성자": {
						"아이디": null
						, "이미지": null
						, "닉네임": null
					}
					, "작성일": null
					, "작성시": null
					, "내용": null
				}
			];
			
			this.clubCommentRequest = {
				"클럽키": null
				, "게시판키": null
				, "댓글키":null
				, "댓글내용":null
			}
		}
		, setClubRequest: function(key, val){
			this.clubRequest[key] = val;
		}
		, getClubRequest: function(key){
			return this.clubRequest[key];
		}
		//동호회 정보
		, getClubInfo: function(){
			var thisClass = this;
			var szBoard_seq = this.clubRequest["게시판키"];
			
			// ajax 시작			
			//parameter setting and run
			document.objGlobal.ajax({
				"sid":"CB001",
				"servicename":"getDetailClubBoard"		
				,"listcount":10
				,"currpageno":1		
			}, {
				"board_seq": szBoard_seq			
				, "modpage_yn": "N"
			},  function(ret){
				
				var msg = document.objGlobal.getResultMsg("getDetailClubBoard", ret["msp"]["contents"]["serviceResult"]);
								
				if(msg) alert(msg); 
				
				// callback and mapping
				if(ret["msp"]["contents"]["items"]){
					thisClass.clubData = {					
						"제목": ret["msp"]["contents"]["items"]["title"]
						, "댓글수": ""
						, "작성자": {
							"아이디": ret["msp"]["contents"]["items"]["reg__id"]
							, "이미지": "../images/board/default_50_50.gif"
							, "닉네임": ret["msp"]["contents"]["items"]["reg__nickname"]
						}
						, "작성일": thisClass.getDateymd(ret["msp"]["contents"]["items"]["reg__date"],'-',8)
						, "작성시":  thisClass.getTimehms(ret["msp"]["contents"]["items"]["reg__time"],':',4)
						, "조회수": ret["msp"]["contents"]["items"]["hit"]
						, "글권한": ""
						, "내용": ret["msp"]["contents"]["items"]["content"]					
					}
				}
				
				$(thisClass).trigger("evtFinishClubInfo");
			});
			// ajax 끝
						
		}
		, getClubData: function(){
			return this.clubData;
		}
		, setClubCommentRequest: function(key, val){
			this.clubCommentRequest[key] = val;
		}
		, getClubCommentRequest: function(key){
			return this.clubCommentRequest[key];
		}
		//댓글리스트
		, getClubCommentList: function(){
			var thisClass = this;
			var szBoard_seq = this.clubCommentRequest["게시판키"];
			
			// ajax 시작			
			//parameter setting and run
			document.objGlobal.ajax({
				"sid":"CB001",
				"servicename":"getClubCommentList"		
				,"listcount":100
				,"currpageno":1		
			}, {
				"board_seq": szBoard_seq							
			}, function(ret){
				
				var msg = document.objGlobal.getResultMsg("getClubBoardList", ret["msp"]["contents"]["serviceResult"]);
				
				if(msg) alert(msg);
				//else return;
				
				// callback and mapping
				thisClass.clubCommentList = [];
								
				if(ret["msp"]["contents"]["com.minigate.sky72.vo.ClubComment"]){					
					for(var i=0; i<ret["msp"]["contents"]["com.minigate.sky72.vo.ClubComment"].length;i++){
						thisClass.clubCommentList.push({					
							"댓글키": ret["msp"]["contents"]["com.minigate.sky72.vo.ClubComment"][i]["reply__seq"]
							, "작성자": {
								"아이디": ret["msp"]["contents"]["com.minigate.sky72.vo.ClubComment"][i]["reg__id"]
								, "이미지": "../images/board/default_50_50.gif"
								, "닉네임": ret["msp"]["contents"]["com.minigate.sky72.vo.ClubComment"][i]["reg__nickname"]
							}
							, "작성일": thisClass.getDateymd((ret["msp"]["contents"]["com.minigate.sky72.vo.ClubComment"][i]["reg__date"]),'-',8)
							, "작성시": thisClass.getTimehms(ret["msp"]["contents"]["com.minigate.sky72.vo.ClubComment"][i]["reg__time"],':',4)
							, "내용": ret["msp"]["contents"]["com.minigate.sky72.vo.ClubComment"][i]["content"]							
						});
					}
				}
				$(thisClass).trigger("evtFinishClubCommentList");
			});
			// ajax 끝
			
		}
		//게시글삭제
		, clubDelete: function(){
			var thisClass = this;
			var szBoard_seq = this.clubRequest["게시판키"];
			
			// ajax 시작			
			//parameter setting and run
			document.objGlobal.ajax({
				"sid":"CB001",
				"servicename":"deleteClubBoard"		
			}, {
				"board_seq": szBoard_seq		
			}, function(ret){
				
				var msg = document.objGlobal.getResultMsg("deleteClubBoard", ret["msp"]["contents"]["serviceResult"]);
				
				if(msg) alert(msg);
				//else return;
				
				$(thisClass).trigger("evtFinishClubDelete");
			});
			// ajax 끝			
			
		}
		, getClubCommentData: function(){
			return this.clubCommentList;
		}
		//댓글삭제
		, clubCommentDelete: function(){
			var thisClass = this;
			var szBoard_seq = this.clubCommentRequest["게시판키"];
			var szComment_seq = this.clubCommentRequest["댓글키"];
			
			// ajax 시작			
			//parameter setting and run
			document.objGlobal.ajax({
				"sid":"CB001",
				"servicename":"deleteClubComment"		
			}, {
				"board_seq": szBoard_seq				
				, "reply_seq": szComment_seq
			}, function(ret){
				
				var msg = document.objGlobal.getResultMsg("clubCommentDelete", ret["msp"]["contents"]["serviceResult"]);
				
				if(msg) alert(msg);
				//else return;
				
				$(thisClass).trigger("evtFinishClubCommentDelete");
			});
			// ajax 끝
		
		}
		//댓글등록
		, clubCommentInsert: function(board_seq, comment_data){
			var thisClass = this;
			var szBoard_seq = this.clubCommentRequest["게시판키"];
			var szComment_content = this.clubCommentRequest["댓글내용"];
						
			// ajax 시작			
			//parameter setting and run
			document.objGlobal.ajax({
				"sid":"CB001",
				"servicename":"regClubComment"		
			}, {
				"board_seq": szBoard_seq				
				, "content": szComment_content
			}, function(ret){
				
				var msg = document.objGlobal.getResultMsg("clubCommentInsert", ret["msp"]["contents"]["serviceResult"]);
				
				if(msg) alert(msg);
				//else return;
				
				$(thisClass).trigger("evtFinishClubCommentInsert");
			});
			// ajax 끝
		}
		, getDateymd: function (n,v,l){			
			n = String(n);
			
			if(n.length < l) return "";
			return n.substr(0,4) +v+ n.substr(4,2) +v+ n.substr(6,2);
		}
		, getTimehms: function(n,v,l){
			n = String(n);
			
			if(n.length < l) return "";
			return n.substr(0,2) +v+ n.substr(2,2) ;
		}
		
	}
	
	
	// 컨트롤
	function clsControl()
	{
		this.objComm = null;
		this.objClubTitle = null;
		this.objClubInfo = null;
		this.objClubCommentList = null;
		this.objAuthButton = null;
		this.objTopMenuInfo = null;
		
		this._init();
	}
	
	clsControl.prototype = {
		_init: function(){
			this.objComm = new clsComm();
			this.objClubTitle = new clsClubTitle();
			this.objClubInfo = new clsClubInfo();
			this.objClubCommentList = new clsClubCommentList();
			this.objAuthButton = new clsAuthButton();
			this.objTopMenuInfo = new clsTopMenuInfo();
			
			this._bind();
			
			// 전체게시글로 default 설정
			this.objClubTitle.setIdx((document.objUtil.getParam("q_my") == "Y") ? 1:0);
			
			// 글정보 받아오기
			if(document.objUtil.getParam("club_seq")){
				
				this.objClubInfo.spinStart();
				
				this.objComm.setClubRequest("클럽키", document.objUtil.getParam("club_seq"));
				this.objComm.setClubRequest("게시판키", document.objUtil.getParam("board_seq"));
				this.objComm.setClubRequest("전체글/내글", document.objUtil.getParam("q_my"));
				this.objComm.getClubInfo();
				
				// 댓글
				this.objClubCommentList.spinStart();
				this.objComm.setClubCommentRequest("게시판키", document.objUtil.getParam("board_seq"));
				this.objComm.getClubCommentList();
			}
		}
		, _bind: function(){
			var thisClass = this;

			//<--navi 페이지 설정 시작
			//홈
			this.objTopMenuInfo.getBtnClubHome().click(function(event){
				event.preventDefault();						
				document.location.href = "home.html?club_seq=" + (thisClass.objComm.getClubRequest("클럽키"));
			});
			//동호회필드
			this.objTopMenuInfo.getBtnClubField().click(function(event){
				event.preventDefault();						
				document.location.href = "club_field.html?club_seq=" + (thisClass.objComm.getClubRequest("클럽키"));
			});
			//일반게시판
			this.objTopMenuInfo.getBtnClubBoard().click(function(event){
				event.preventDefault();						
				document.location.href = "club_list.html?club_seq=" + (thisClass.objComm.getClubRequest("클럽키"));
			});
			//사진게시판
			this.objTopMenuInfo.getBtnClubPic().click(function(event){
				event.preventDefault();						
				document.location.href = "club_picture_list.html?club_seq=" + (thisClass.objComm.getClubRequest("클럽키"));
			});
			//관리하기
			this.objTopMenuInfo.getBtnClubAdmin().click(function(event){
				event.preventDefault();						
				document.location.href = "club_admin_info.html?club_seq=" + (thisClass.objComm.getClubRequest("클럽키"));
			});
			//navi 페이지 설정 시작 -->
			
			// 글정보 완료
			$(this.objComm).bind("evtFinishClubInfo", function(){
				var thisData = this.getClubData();
				
				thisClass.objClubInfo.spinEnd();
				thisClass.objClubInfo.setInfo(thisData);
				
				//thisClass.objAuthButton.setAuth(document.objUtil.getCookie("member_id") == thisData["작성자"]["아이디"]);
				thisClass.objAuthButton.setAuth(true);
			// 댓글조회완료
			}).bind("evtFinishClubCommentList", function(){
				var thisData = this.getClubCommentData();
								
				thisClass.objClubCommentList.spinEnd();
				thisClass.objClubCommentList.setList(thisData);
			// 삭제완료
			}).bind("evtFinishClubDelete", function(){
				var ret = arguments[1];
				
				document.objGlobal.spinEnd();
				
				document.location.href = "club_list.html?club_seq="+ (thisClass.objComm.getClubRequest("클럽키"));
						
			// 댓글삭제완료
			}).bind("evtFinishClubCommentDelete", function(){
				var ret = arguments[1];
				
				document.objGlobal.spinEnd();
				
				// 댓글
				thisClass.objClubCommentList.spinStart();				
				thisClass.objComm.getClubCommentList();
			
						
			// 댓글등록완료
			}).bind("evtFinishClubCommentInsert", function(){
				var ret = arguments[1];
				
				document.objGlobal.spinEnd();
				
				// 댓글조회
				thisClass.objClubCommentList.setCommentSubmitData("");
				thisClass.objClubCommentList.spinStart();				
				thisClass.objComm.getClubCommentList();
			});
			
			// 글쓰기
			$("div#container section#main_content div.golf_board a").has("img[alt=글쓰기]").click(function(event){
				event.preventDefault();
				
				document.location.href = "club_write.html?club_seq=" + (thisClass.objComm.getClubRequest("클럽키")) + "&q_my=" + (thisClass.objComm.getClubRequest("전체글/내글"));
			});
			
			// 목록
			$("div#container section#main_content div.golf_board a").has("img[alt=목록]").click(function(event){
				event.preventDefault();
				
				document.location.href = "club_list.html?club_seq=" + (thisClass.objComm.getClubRequest("클럽키")) + "&q_my=" + (thisClass.objComm.getClubRequest("전체글/내글"));
			});
			
			// 수정
			this.objAuthButton.$getBtnModify().click(function(event){
				event.preventDefault();
				
				document.location.href = "club_write.html?club_seq=" + (thisClass.objComm.getClubRequest("클럽키")) + "&board_seq="+ (thisClass.objComm.getClubRequest("게시판키")) + "&q_my="+ (thisClass.objComm.getClubRequest("전체글/내글")) ;
			});
			
			// 삭제
			this.objAuthButton.$getBtnDelete().click(function(event){
				event.preventDefault();
				
				if(!confirm("삭제하시겠습니까?")) return;
				
				document.objGlobal.spinStart("deleting...");
				thisClass.objComm.clubDelete();
			});
			
			// 댓글삭제
			this.objClubCommentList.$getBtnCommentDelete().live("click", function(event){
				event.preventDefault();
				
				var n = thisClass.objClubCommentList.$getBtnCommentDelete().index($(this));
				var board_seq = document.objUtil.getParam("board_seq");
				var comment_seq = (thisClass.objClubCommentList.getCommentData(n)["댓글키"]);
				
				thisClass.objComm.setClubCommentRequest("댓글키",comment_seq);
				
				if(!confirm("삭제하시겠습니까?")) return;
				
				document.objGlobal.spinStart("deleting...");
				thisClass.objComm.clubCommentDelete();
			});
			
			// 댓글등록
			this.objClubCommentList.$getBtnCommnetSubmit().click(function(event){
				event.preventDefault();
				
				var comment_data = (thisClass.objClubCommentList.getCommentSubmitData());
				thisClass.objComm.setClubCommentRequest("댓글내용",comment_data);
								
				if(!comment_data) {
					alert("입력된 댓글내용이 없습니다.");
					return;
				}
				
				document.objGlobal.spinStart("inserting...");
				thisClass.objComm.clubCommentInsert();
			});
		}
	}
	
	document.objControl = new clsControl();
});